#define  _CRT_SECURE_NO_WARNINGS 1
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */
typedef struct ListNode ListNode;
struct ListNode* mergeTwoLists(struct ListNode* list1, struct ListNode* list2) {
    ListNode* pcur1, * pcur2, * newHead, * newTail;
    pcur1 = list1;
    pcur2 = list2;
    ListNode* mid = (ListNode*)malloc(sizeof(ListNode));
    if (mid == NULL)
        perror("malloc");
    newHead = newTail = mid;
    while (pcur1 && pcur2)
    {
        if (pcur1->val <= pcur2->val)
        {
            newTail->next = pcur1;
            newTail = newTail->next;
            pcur1 = pcur1->next;
        }
        else
        {
            newTail->next = pcur2;
            newTail = newTail->next;
            pcur2 = pcur2->next;
        }
    }
    if (pcur1 == NULL)
    {
        newTail->next = pcur2;
    }
    else
    {
        newTail->next = pcur1;
    }
    ListNode* ret = newHead->next;
    free(newHead);
    newHead = NULL;
    return ret;

}